package com.cqie.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.rbac.bo.PageRoleBo;
import com.cqie.rbac.bo.RoleStatusBo;
import com.cqie.rbac.service.ISysRoleService;
import com.cqie.rbac.vo.RoleVo;
import com.cqie.rbac.bo.RoleUpOrAddBo;
import com.cqie.rbac.entity.SysRole;
import com.cqie.rbac.exception.BusinessException;
import com.cqie.rbac.mapper.SysRoleMapper;
import com.cqie.rbac.util.Result;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色实体 服务实现类
 * </p>
 *
 * @author 夏兵
 * @since 2021-09-02
 */
@Service
@Log4j
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public Result getRole() {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRole::getStatus, 0);
        List<SysRole> roleList = sysRoleMapper.selectList(queryWrapper);
        List<RoleVo> roleVos = new ArrayList<>(roleList.size());
        roleList.forEach((r) -> {
            roleVos.add(new RoleVo(r.getId(), r.getName()));
        });
        return Result.ok().data(roleVos);
    }

    @Override
    public Result pageOfRole(PageRoleBo pageRoleBo, Integer current, Integer size) {
        Page<SysRole> sysRolePage;
        if (current != null && current > 0 && size != null && size > 0) {
            sysRolePage = new Page<>(current, size);
        } else {
            sysRolePage = new Page<>();
        }
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRole::getStatus, pageRoleBo.getStatus());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(pageRoleBo.getName()), SysRole::getName, pageRoleBo.getName());
        return Result.ok().data(sysRoleMapper.selectPage(sysRolePage, lambdaQueryWrapper));
    }

    @Override
    public Result changeRoleStatus(RoleStatusBo roleStatusBo) {
        if (roleStatusBo.getId() == null || roleStatusBo.getStatus() == null) {
            log.error("改变状态必须有角色id和status");
            throw new BusinessException("5000", "改变状态必须有角色id和status");
        }
        SysRole sysRole = new SysRole();
        sysRole.setId(roleStatusBo.getId());
        sysRole.setStatus(roleStatusBo.getStatus());
        sysRoleMapper.updateById(sysRole);
        return Result.ok();
    }

    @Override
    @Transactional
    public Result multiChangeRoleStatus(List<RoleStatusBo> roleStatusBos) {
        for (RoleStatusBo bo : roleStatusBos) {
            SysRole sysRole = new SysRole();
            sysRole.setStatus(bo.getStatus());
            sysRole.setId(bo.getId());
            sysRoleMapper.updateById(sysRole);
        }
        return Result.ok();
    }

    @Override
    @Transactional
    public Result multiDelRole(Long[] idArray) {
        for (long id : idArray) {
            sysRoleMapper.deleteById(id);
            sysRoleMapper.deleteUserRole(id);
        }
        return Result.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result roleUpOrAdd(RoleUpOrAddBo roleUpOrAddBo) {
        SysRole sysRole = new SysRole();
        sysRole.setName(roleUpOrAddBo.getName());
        sysRole.setStatus(roleUpOrAddBo.getStatus());
        if (roleUpOrAddBo.getTag() == 1 || !roleUpOrAddBo.getName().equals(roleUpOrAddBo.getOriginName())) {
            LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysRole::getName, sysRole.getName());
            if (sysRoleMapper.selectOne(queryWrapper) != null) {
                log.error("该角色名已存在，新增失败");
                throw new BusinessException("5000", "该角色名已存在");
            }
        }
        if (roleUpOrAddBo.getTag() == 1) {
            sysRoleMapper.insert(sysRole);
        } else {
            sysRole.setId(roleUpOrAddBo.getId());
            sysRoleMapper.updateById(sysRole);
            sysRoleMapper.deleteRoleMenu(sysRole.getId());
        }
        for (Long menuId : roleUpOrAddBo.getMenus()) {
            if (sysRoleMapper.addRoleMenu(sysRole.getId(), menuId) < 1) {
                throw new BusinessException("5000", "角色菜单新增失败");
            }
        }
        return Result.ok();
    }
}
